package com.hepinghospital.user.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.ui.activity.userinfo.SearchHelpActivity;
import com.hepinghospital.user.util.GlideUtil;
import com.rest.response.HomeDeptResponse;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import jz.joyoung.robot.util.UtilString;


/**
 * @time 2020/11/18 14:18
 */

public class HelpListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public Context mContext;
    public List<HomeDeptResponse> list;


    public HelpListAdapter(Context context, List<HomeDeptResponse> list) {
        this.mContext = context;
        this.list = list;
    }

    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from( context )
                .inflate( R.layout.item_help_list, parent, false );
        return new ViewHolder( view );

    }

    @Override
    public void onBindViewHolder(@NotNull RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder myHolder = (ViewHolder) holder;

        myHolder.tvQuestionName.setText( list.get( position ).name );
        if (!UtilString.isEmpty( list.get( position ).url )) {
            GlideUtil.loadImage( mContext, list.get( position ).url, myHolder.ivQuestionType );
        } else {
            myHolder.ivQuestionType.setImageResource( R.mipmap.icon_help_zhang );
        }
        myHolder.itemView.setOnClickListener( v -> {
            Intent intent = new Intent( mContext, SearchHelpActivity.class );
            intent.putExtra( "helpName", list.get( position ).name );
            intent.putExtra( "helpCode", list.get( position ).code );
            mContext.startActivity( intent );

        } );


    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_question_type)
        ImageView ivQuestionType;
        @BindView(R.id.tv_question_name)
        TextView tvQuestionName;

        public ViewHolder(View itemView) {
            super( itemView );
            ButterKnife.bind( this, itemView );
        }
    }

}
